//
//  XMGTabBarController.m
//  01-百思不得姐
//
//  Created by xiaomage on 15/7/22.
//  Copyright (c) 2015年 小码哥. All rights reserved.
//

#import "XMGTabBarController.h"
#import "WYCNavigationController.h"
#import "WYCMeController.h"
#import "WYCBillController.h"
#import "WYCSummaryController.h"
#import "WYCRepaymentController.h"
#import "ADTabBar.h"
#import "UIColor+ColorHelper.h"
#import "ADAnimationView.h"
#import "WYCmyCardCotroller.h"
@interface XMGTabBarController () <ADTabBarDelegate>
@property(nonatomic,strong)NSArray *normalImageArray;
@property(nonatomic,strong)NSArray *selectedImageArray;
@property(nonatomic,strong)NSArray *titleArray;

@end

@implementation XMGTabBarController
@synthesize button;



- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadversion];
//    [self setup];
//    [self addButtonNotifation];
    
    
    
}
- (void)createTabBar{
    
    
    
}


//tabbar加阴影
- (void)dropShadowWithOffset:(CGSize)offset radius:(CGFloat)radius color:(UIColor *)color opacity:(CGFloat)opacity
{
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, self.tabBar.bounds);
    self.tabBar.layer.shadowPath = path;
    CGPathCloseSubpath(path);
    CGPathRelease(path);
    
    self.tabBar.layer.shadowColor = color.CGColor;
    self.tabBar.layer.shadowOffset = offset;
    self.tabBar.layer.shadowRadius = radius;
    self.tabBar.layer.shadowOpacity = opacity;
    
    self.tabBar.clipsToBounds = NO;
}


#pragma mark - ADTabBarDelegate
- (void)tabBarDidClickCenterButton:(ADTabBar *)tabBar
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    ADAnimationView *adView = [[ADAnimationView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    [window addSubview:adView];
    [adView showButtonAnimation];
}

-(void)loadversion
{
    
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
    NSLog(@"=======%@",appVersion);
    params[@"version"] = appVersion;
    
    [HttpTool POST:function params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue ] == 0) {
            NSLog(@"第一%@",responseObject);
            if ([[responseObject objectForKey:@"result"] intValue ] == 0) {
                
                _normalImageArray = @[@"账单管理_normal", @"汇总_normal", @"我的_normal"];
                _selectedImageArray = @[@"账单管理_selected", @"汇总_selected", @"我的_selected"];
                _titleArray = @[@"账单", @"统计", @"我的"];
                self.tabBar.tintColor = [UIColor whiteColor];
                //更换系统自带的tabbar
                ADTabBar *tabBar = [[ADTabBar alloc]init];
                tabBar.backgroundColor = [UIColor whiteColor];
                
                CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width);
                
                UIGraphicsBeginImageContext(rect.size);
                
                CGContextRef context = UIGraphicsGetCurrentContext();
                
                CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
                
                CGContextFillRect(context, rect);
                
                UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
                
                [tabBar setBackgroundImage:img];
                
                [tabBar setShadowImage:img];
                
                tabBar.adDelegate = self;
                //使用kvc，将自定义的tabbar替换掉系统的tabbar
                [self setValue:tabBar forKey:@"tabBar"];
                
                
                //tabbar加阴影
                [self dropShadowWithOffset:CGSizeMake(0, -1) radius:1 color:[UIColor grayColor] opacity:0.3];
                
                
                WYCBillController *homeVC = [[WYCBillController alloc] init];
                WYCSummaryController *msgVC = [[WYCSummaryController alloc] init];
                WYCMeController *mineVC = [[WYCMeController alloc] init];
                
                NSMutableArray *controllerArray = [NSMutableArray arrayWithObjects:homeVC, msgVC, mineVC, nil];
                
                for (int  i = 0; i < controllerArray.count; i++)
                {
                    // 分别找到每个视图控制器
                    UIViewController *vc = controllerArray[i];
                    // 添加标题
                    vc.title = _titleArray[i];
                    // 转化成导航控制器
                    WYCNavigationController *navi = [[WYCNavigationController alloc]initWithRootViewController:vc];
                    // 将数组array中的视图控制器替换成导航控制器
                    [controllerArray replaceObjectAtIndex:i withObject:navi];
                    
                    // 渲染模式
                    UIImage *normalImage = [UIImage imageNamed:_normalImageArray[i]];
                    UIImage *selectImage = [UIImage imageNamed:_selectedImageArray[i]];
                    
                    navi.tabBarItem.image = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
                    navi.tabBarItem.selectedImage = [selectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
                    
                    //tabbar文字字体颜色
                    [navi.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:wycMain} forState:UIControlStateSelected];
                    
                    //文字位置
                    [navi.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -3)];
                    
                }
                self.viewControllers = controllerArray;

            }
            else
            {
                _normalImageArray = @[@"账单管理_normal", @"卡包_normal", @"汇总_normal", @"我的_normal"];
                _selectedImageArray = @[@"账单管理_selected", @"卡包_selected", @"汇总_selected", @"我的_selected"];
                _titleArray = @[@"账单", @"办卡", @"统计", @"我的"];
                self.tabBar.tintColor = [UIColor whiteColor];
                //更换系统自带的tabbar
                ADTabBar *tabBar = [[ADTabBar alloc]init];
                tabBar.backgroundColor = [UIColor whiteColor];
                
                CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width);
                
                UIGraphicsBeginImageContext(rect.size);
                
                CGContextRef context = UIGraphicsGetCurrentContext();
                
                CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
                
                CGContextFillRect(context, rect);
                
                UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
                
                [tabBar setBackgroundImage:img];
                
                [tabBar setShadowImage:img];
                
                tabBar.adDelegate = self;
                //使用kvc，将自定义的tabbar替换掉系统的tabbar
                [self setValue:tabBar forKey:@"tabBar"];
                
                
                //tabbar加阴影
                [self dropShadowWithOffset:CGSizeMake(0, -1) radius:1 color:[UIColor grayColor] opacity:0.3];
                
                
                WYCBillController *homeVC = [[WYCBillController alloc] init];
                WYCmyCardCotroller *dynamicVC = [[WYCmyCardCotroller alloc] init];
                WYCSummaryController *msgVC = [[WYCSummaryController alloc] init];
                WYCMeController *mineVC = [[WYCMeController alloc] init];
                
                NSMutableArray *controllerArray = [NSMutableArray arrayWithObjects:homeVC, dynamicVC, msgVC, mineVC, nil];
                
                
                
                for (int  i = 0; i < controllerArray.count; i++)
                {
                    // 分别找到每个视图控制器
                    UIViewController *vc = controllerArray[i];
                    // 添加标题
                    vc.title = _titleArray[i];
                    // 转化成导航控制器
                    WYCNavigationController *navi = [[WYCNavigationController alloc]initWithRootViewController:vc];
                    // 将数组array中的视图控制器替换成导航控制器
                    [controllerArray replaceObjectAtIndex:i withObject:navi];
                    
                    // 渲染模式
                    UIImage *normalImage = [UIImage imageNamed:_normalImageArray[i]];
                    UIImage *selectImage = [UIImage imageNamed:_selectedImageArray[i]];
                    
                    navi.tabBarItem.image = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
                    navi.tabBarItem.selectedImage = [selectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
                    
                    //tabbar文字字体颜色
                    [navi.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:wycMain} forState:UIControlStateSelected];
                    
                    //文字位置
                    [navi.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -3)];
                    
                }
                self.viewControllers = controllerArray;
            }
            
        }
        
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
        [self showToast:Failed];
    }];
}
@end
